/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.incremental;

import java.util.Collection;
import javax.swing.JOptionPane;
import syntax.Location;
import syntax.Message;
import syntax.Repository;
import syntax.Transition;

/**
 *
 * @author ennouaary
 */
public class JMessageDialog extends javax.swing.JDialog {

    Repository repository;
    //Transition transition;
    TransitionObservable observable;
    Message message;
    Collection<Message> messages;
    boolean newNotEdit = true;

    /**
     * Creates new form JMessageDialog
     */
    public JMessageDialog(TransitionObservable observable, Collection<Message> messages) {
        this.observable = observable;
        //repository = Repository.getInstance();
        this.messages = messages;
        //this.transition = observable.getFrame().getTransition();
        message = new Message(null);
        this.observable = observable;
        newNotEdit = true;
        initComponents();
        fillInComponents();
        jActionLabel.setText("New Message");
    }

    public JMessageDialog(Message message, TransitionObservable observable, Collection<Message> messages) {
        this.observable = observable;
        this.message = message;
        //repository = Repository.getInstance();
        this.messages = messages;
        //this.transition = observable.getFrame().getTransition();
        this.observable = observable;
        newNotEdit = false;
        initComponents();
        fillInComponents();
        jActionLabel.setText("Edit Message");
    }

    private void fillInComponents() {
        jNameTextField.setText(message.getName());
        if(message.isInput()) {
            jInputRadioButton.setSelected(true);
        }
        else if (message.isOutput()) {
            jOutputRadioButton.setSelected(true);
        }
    }
    
    private boolean isNameDuplicated(String name) {
        boolean res = false;
        if(name.compareTo(message.getName())==0) {
            return false;
        }
        for (Message message : messages) {
            if (message.getName().compareTo(name) == 0) {
                res = true;
                break;
            }
        }
        return res;
    }

    private String getSymbol() {
        return jNameTextField.getText();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jMessageTypeButtonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jMessageTypePanel = new javax.swing.JPanel();
        jInputRadioButton = new javax.swing.JRadioButton();
        jOutputRadioButton = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jOkButton = new javax.swing.JButton();
        jCancelButton = new javax.swing.JButton();
        jResetButton = new javax.swing.JButton();
        jActionLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jNameTextField = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();

        setTitle("Symbol");
        setAlwaysOnTop(true);
        setModal(true);
        getContentPane().setLayout(new java.awt.FlowLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jMessageTypePanel.setLayout(new java.awt.GridLayout(1, 0));

        jMessageTypeButtonGroup.add(jInputRadioButton);
        jInputRadioButton.setSelected(true);
        jInputRadioButton.setText("Input");
        jInputRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jInputRadioButtonActionPerformed(evt);
            }
        });
        jMessageTypePanel.add(jInputRadioButton);

        jMessageTypeButtonGroup.add(jOutputRadioButton);
        jOutputRadioButton.setText("Output");
        jMessageTypePanel.add(jOutputRadioButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(jMessageTypePanel, gridBagConstraints);

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jOkButton.setText("OK");
        jOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOkButtonActionPerformed(evt);
            }
        });
        jPanel2.add(jOkButton);

        jCancelButton.setText("Cancel");
        jCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelButtonActionPerformed(evt);
            }
        });
        jPanel2.add(jCancelButton);

        jResetButton.setText("Reset");
        jResetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jResetButtonActionPerformed(evt);
            }
        });
        jPanel2.add(jResetButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(jPanel2, gridBagConstraints);

        jActionLabel.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        jActionLabel.setText("New Message");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(jActionLabel, gridBagConstraints);

        jLabel2.setText("Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        jPanel1.add(jLabel2, gridBagConstraints);

        jNameTextField.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        jPanel1.add(jNameTextField, gridBagConstraints);

        jPanel3.setPreferredSize(new java.awt.Dimension(30, 30));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 30, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 30, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(jPanel3, gridBagConstraints);

        getContentPane().add(jPanel1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jInputRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jInputRadioButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jInputRadioButtonActionPerformed

    private void jOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOkButtonActionPerformed
        // TODO add your handling code here:
        String symbol = getSymbol();
        if (symbol == null || symbol.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Message Symbol Required", "Message Symbol Required", JOptionPane.ERROR_MESSAGE);
            return;
        }
//        if (!(repository.getMessage(symbol) == null)) {
        if (isNameDuplicated(symbol)) {
            JOptionPane.showMessageDialog(this, "Message Symbol Duplicated", "Message Symbol Duplicated", JOptionPane.ERROR_MESSAGE);
            return;
        } else {
            // add the new symbol to repository            
            if (jInputRadioButton.isSelected()) {
                message.setInput();
                //transition.setMessage(repository.addInput(symbol));
            } else if (jOutputRadioButton.isSelected()) {
//                transition.setMessage(repository.addOutput(symbol));
                message.setOutput();
            }
            //transition.setMessage(message);
            message.setName(symbol);
            observable.setResult(message);
            if (newNotEdit) {
                messages.add(message);
                JOptionPane.showMessageDialog(this, "New Transition Message Created");
            } else {
                JOptionPane.showMessageDialog(this, "Transition Message Changed");
            }
            observable.notifyTransitionChange();
            dispose();
        }
    }//GEN-LAST:event_jOkButtonActionPerformed

    private void jCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelButtonActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jCancelButtonActionPerformed

    private void jResetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jResetButtonActionPerformed
        // TODO add your handling code here:
        reset();
    }//GEN-LAST:event_jResetButtonActionPerformed

    private void reset() {
        jNameTextField.setText("");
        jInputRadioButton.setSelected(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JMessageDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JMessageDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JMessageDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JMessageDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JMessageDialog(null, null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jActionLabel;
    private javax.swing.JButton jCancelButton;
    private javax.swing.JRadioButton jInputRadioButton;
    private javax.swing.JLabel jLabel2;
    private javax.swing.ButtonGroup jMessageTypeButtonGroup;
    private javax.swing.JPanel jMessageTypePanel;
    private javax.swing.JTextField jNameTextField;
    private javax.swing.JButton jOkButton;
    private javax.swing.JRadioButton jOutputRadioButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton jResetButton;
    // End of variables declaration//GEN-END:variables
}
